package com.wrial.offer;
/*
 * @Author  Wrial
 * @Date Created in 17:10 2020/3/31
 * @Description  链表中倒数第k个节点

给定一个链表: 1->2->3->4->5, 和 k = 2.
返回链表 4->5.
 */

public class FaceTest_22 {


    // 如果链表只有k个节点，那就是原链表
    // 设置两个指针 p1  p2  p2
    public ListNode getKthFromEnd(ListNode head, int k) {

        if (k == 0) return null;
        ListNode curP1 = head;
        ListNode curP2 = head;
        int countP2 = k;
        while (curP1 != null) {
            if (countP2 <= 0) curP2 = curP2.next;
            curP1 = curP1.next;
            countP2--;
        }
        return  curP2;
    }

    public class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
        }
    }
}
